package com.martinzhuang.scan_sd_card;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {

    private EditText edt_suffix;
    private Button btn_search, btn_recyclerview;
    public static List<File> mfileList;
    private CustomProgressDialog customProgressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mfileList = new ArrayList<File>();
        edt_suffix = (EditText) findViewById(R.id.edt_suffix);
        btn_search = (Button) findViewById(R.id.btn_search);
        btn_search.setOnClickListener(new searchFiles());
        btn_recyclerview = (Button) findViewById(R.id.btn_recyclerview);
        btn_recyclerview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SampleActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        edt_suffix.setText("");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private class searchFiles implements View.OnClickListener {

        @Override
        public void onClick(View view) {

            new AsyncTask<Integer, Integer, String[]>() {
//                ProgressDialog progressDialog;


                //run in start
                @Override
                protected void onPreExecute() {
//                    progressDialog = ProgressDialog.show(MainActivity.this, "Message", "Scanning...");
                    startProgressDialog();
                    super.onPreExecute();
                }

                //run in the background
                @Override
                protected String[] doInBackground(Integer... integers) {
                    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                        Toast.makeText(MainActivity.this, "There is no SdCard...", Toast.LENGTH_SHORT).show();
                    } else {
                        if (!("".equals(edt_suffix.getText().toString()))) {
                            getFiles(Environment.getExternalStorageDirectory());
                            System.out.println("Background:" + Environment.getExternalStorageDirectory());
                        }
                    }
                    return null;
                }

                //task finish
                @Override
                protected void onPostExecute(String[] s) {
//                    progressDialog.dismiss();
                    stopProgressDialog();
                    if (edt_suffix.getText().toString().equals("")) {
                        Toast.makeText(MainActivity.this, "Please Input Valid Suffix...", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, "Scan Finished...", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(MainActivity.this, ListOk.class);
                        startActivity(intent);
                    }
                    super.onPostExecute(s);
                }
            }.execute(0);
        }
    }

    //filePath is the root directory of sdcard
    public void getFiles(File filePath) {
        File[] fileList = filePath.listFiles();

        for (int i = 0; i < fileList.length; i++) {
            if (fileList[i].isDirectory()) {
                getFiles(fileList[i]);
            } else {
                if (fileList[i].getName().endsWith("." + edt_suffix.getText().toString().trim())) {
                    mfileList.add(fileList[i]);
                }
            }
        }
    }

    private void startProgressDialog() {
        if (customProgressDialog == null) {
            customProgressDialog = CustomProgressDialog.createDialog(this);
            customProgressDialog.setMessage("Scanning...");
        }

        customProgressDialog.show();
    }

    private void stopProgressDialog() {
        if (customProgressDialog != null) {
            customProgressDialog.dismiss();
            customProgressDialog = null;
        }
    }

}
